<template>
  <div class="detail-container">
    <div class="header-nav">
      <div class="logo">“沈誉通”信用生活综合系统</div>
      <div class="nav">
        <!-- <router-link to="/home">首页</router-link> -->
        <router-link to="/credit-life">信用生活</router-link>
        <router-link to="/credit-business">信用商业</router-link>
        <router-link to="/behavior">亲社会行为</router-link>
        <router-link to="/news">新闻资讯</router-link>
        <router-link to="/credit-management">信用管理</router-link>
      </div>
    </div>
    <div class="back-bar">
      <el-button icon="el-icon-arrow-left" @click="$router.push('/credit-life')" type="text">返回信用生活</el-button>
    </div>
    <div class="detail-main">
      <h2>便捷泊车</h2>
      <div class="car-plate-section">
        <el-input v-model="carPlate" placeholder="请输入车牌号" class="car-plate-input" maxlength="8" />
      </div>
      <div class="parking-list">
        <div v-for="parking in parkings" :key="parking.id" class="parking-card">
          <div class="parking-info">
            <h3>{{ parking.name }}</h3>
            <p class="address">地址：{{ parking.address }}</p>
            <p class="remain">剩余车位：{{ parking.remain }}</p>
          </div>
          <el-button type="primary" :disabled="!carPlate || parking.appointed" @click="appoint(parking)">
            {{ parking.appointed ? '已预约' : '预约车位' }}
          </el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "ParkingDetail",
  data() {
    return {
      carPlate: '',
      parkings: [
        {
          id: 1,
          name: '西湖地下停车场',
          address: '杭州市西湖区灵隐路88号',
          remain: 12,
          appointed: false
        },
        {
          id: 2,
          name: '市民中心停车场',
          address: '杭州市江干区市民街1号',
          remain: 5,
          appointed: false
        },
        {
          id: 3,
          name: '武林广场停车场',
          address: '杭州市下城区武林广场',
          remain: 0,
          appointed: false
        }
      ]
    };
  },
  created() {
    this.recordClick();

  },
  methods: {
    async recordClick() {
      try {
        const { data: userData } = await axios.get('/users/user/basic', {
          headers: { Authorization: `Bearer ${localStorage.getItem('token')}` },
        });
        if (userData.success && userData.data) {
          this.user = userData.data;
        } else {
          this.$message.error('获取用户信息失败：无有效数据');
        }
      } catch (error) {
        this.$message.error('获取用户信息失败');
        console.error('获取用户信息失败:', error);
      }
      const userId = this.user.id;

      axios.post(`/commodity/click/parking?userId=${userId}`, {
        headers: { Authorization: `Bearer ${localStorage.getItem('token')}` },
      });
    },
    appoint(parking) {
      if (parking.appointed || !this.carPlate) return;
      this.$set(parking, 'appointed', true);
      this.$message.success(`已成功预约${parking.name}，车牌号：${this.carPlate}`);
    }
  }
}
</script>

<style scoped>
.detail-container {
  min-height: 100vh;
  background: radial-gradient(ellipse at center, #001e3c 0%, #000 100%);
  color: #fff;
}
.header-nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 30px;
  background: #002d52;
}
.logo {
  font-size: 20px;
  color: #4fc3f7;
}
.nav {
  display: flex;
  gap: 20px;
}
.nav a {
  color: #fff;
  text-decoration: none;
  padding: 5px 10px;
  border-radius: 4px;
  transition: all 0.3s ease;
}
.nav a:hover {
  color: #4fc3f7;
}
.back-bar {
  margin: 20px 0 0 0;
  max-width: 800px;
  margin-left: auto;
  margin-right: auto;
}
.detail-main {
  max-width: 900px;
  margin: 40px auto;
  background: #13263a;
  border-radius: 10px;
  padding: 40px 30px;
  box-shadow: 0 5px 15px rgba(79, 195, 247, 0.1);
}
.car-plate-section {
  max-width: 400px;
  margin: 0 auto 30px;
}
.car-plate-input {
  width: 100%;
}
.parking-list {
  display: flex;
  flex-direction: column;
  gap: 30px;
}
.parking-card {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: #0a2744;
  border-radius: 10px;
  padding: 24px 30px;
  border: 1px solid #1e88e5;
}
.parking-info h3 {
  color: #4fc3f7;
  margin-bottom: 8px;
  font-size: 20px;
}
.parking-info .address,
.parking-info .remain {
  color: #b0bec5;
  font-size: 15px;
  margin-bottom: 4px;
}
</style>
